package com.su.gulimall.order.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.su.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000121614488";
    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDE4jtbxKqaT2KVs1XSlZjjTWfD2Hv+JjM9VHwJFYBj8QQAIubVgPOk9p3Kx1yZhDQHsNEbpnGLe29vTsHtI6XLPb6sa6lJjUToMO5XHFeNw2ldB56b8PF7UJlhZjXuZCZvyOLjXA4hW0vXyM1OpbZVVigfri8QWYL38Zs1Rv45ID3ColKOkhGvNUd88EujU0+/0gn1sN4BNraxUx7ldZT7En07Jhz0XA5SVmJ0bAOPTew2rprt4aKz62SMDKaJWk+c8zmjmyhclLQsgC2ZvTrisw0a0u0mb2GZEplVm85tSvjYcKhwPMSeQQjRT7gmhWg5bqShDNgqVAyiCAEemLdRAgMBAAECggEAS/UL+YeSWh4uZprdjXTxmyAN3Fz02URbmFmz0L1e3y/o3zSX4d7yIL6UedsYOpHqkN0Cj6xTOgDRKRw1bQvHtvWHTnXqxdmesAXQrgLdNHAjgiSiaH84at7Zjd4lCGfS/GWAQz8MpInYZNOvgZwlEB9BM5e7yI8AHDA2NQPCqL692ZZk/hkHs/qCvg4ScQEcADLcM43SSUEvggiyx5lAWde2nVmWjG3/7eR0B1I3OBsuN2EDL46hcpnDtqpKtxcMbEkSNvFakopY41IwOllzA2EpcGtp2HrF0T1Ts16ixszJ/39L9cc18TRRepNafUDdfQTRmvOTcWFOGFdcq/TFkQKBgQDySBCRGVTTDK7MF51+YXFUjCwuwwkVrRbZJZjFjBn6cO8pkCiIdYsg6PskTUp7BBEacUuBrmvFruMnGWj0p80heoGZNakL2cE7k+YkwpeESMz4iur/9ETOPfiS+rvswnje9BbEX+mFBGaHpK4UgnYo0RdgZMf0E4HI962Ej8v5XwKBgQDQCB02F760Zf77c2i/am1+SWhp74bMBgFm2TPCcX7W0ApLEjkI8+VV8WRLmeKSHm40r7o6fhhrfGS36kvgheHe16p2l8laZV+Q627yeqDmdrPaJS4XCJEOwk3a7pg3JeeEfCG5FRzRwtEKevMnn7uMDgsPQ4wQypfJqbswqvkdTwKBgAj9o5++0OcEEXiBZ9bIxuvcR8d0dWHYioCEZKbwDAa/KiDguKBXvFH8U1WFiL4H6JqSmbq2PkmXs9Y/myV5FKOpaCXHV/6YlhMf6epzTiwah4CxpwbYibUdl5vcshyXGuUrKa0f4gqNn0BpXAxmyvu4xqVNGB52mtpaVpYUv/0BAoGAJqF7GzJpnYJbSUJzLyiKUKs9En8CXxy2Yezvqan9zdIwNySZJspzyfPblhZrdorkGDCRuo7z5fHhmi2ODZGPEhymcQNaxsiQaI7vuWOAQYvFdjr1Rh23IjB+2Kr55TPKvFV4xa/f/CvbOcD/KlqFL0rkTt9SQUP9z9sPAcieHg0CgYEAoxeP/5ynSFXTDCHH3HsaJ6zAQJZA1o30AFQoUMW/kBQYxdiLyWRcOUXIKQvz17Xclyaz5dEfGCqV26atY4BlSjPav1OClH6voiMaGBeyyUqyIwUe4SSpXKdUuvSdHoIwHomQuRg0QIM0p7+mPZRv/HmuIrjg6S5MsV1W4oAnEDU=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsKp9I28aV1JRvMjjDbhuHYSggT7c0lJuAtS8iyWBzfrZ+SUOVyiF+SubY/F7e5g5+w9exvJP5r7br1KVWP0wenZlvB441frtHYPKRS4H8F/8mRSSGWIdi55esFzfO+WB4gzvQmYemeC6qmdnWdPBRgFYVLz/H9NeEm7QVAJ36IJ/u1TYmrk+CnanjUYufwYNGrbIP4N2DaWWvtdBijHZ3CVprcmzGfGER5pLae4Jovo9+ciW9V4s30sLnQSNncjFdFG4QnwtUp6vGh6Dw96NfX/pio1gD0XMx0WwXidDKgfsG7zE1va4WtRY67pooIRqzDDHx5uF/6FgvtvulAzBGQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息，最大努力通知型解决方案
    private  String notify_url="http://gulimallsu.natapp1.cc/payed/notify";
    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";
    // 签名方式
    private  String sign_type = "RSA2";
    // 字符编码格式
    private  String charset = "utf-8";
    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);
        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);
        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"9m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();
        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        return result;

    }
}
